package com.cassiopae.services;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cassiopae.dao.ProfileDaoImpl;
import com.cassiopae.dao.UserDaoImpl;
import com.cassiopae.entity.User;
import com.cassiopae.util.CassiopaeDumpConstant;
import com.cassiopae.util.CassiopaeDumpUtil;
/**
 * Servlet implementation class Connexion
 */
@WebServlet("/ConnexionServlet")
public class ConnexionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	UserDaoImpl daoImpl = new UserDaoImpl();
	ProfileDaoImpl profileDaoImpl = new ProfileDaoImpl();


	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		User userConnected = null;
		
		userConnected = (User) request.getSession().getAttribute("user");
		
		if(userConnected!=null && userConnected.getActivateOrNot()){
			daoImpl.retrieveAllProfiles(userConnected);
			request.getSession().setAttribute("LISTPROFIL", userConnected.getProfiles());
			
			String path = getServletContext().getRealPath("")+"/WEB-INF/config/";
			CassiopaeDumpUtil.pathToConfigFile = path;
			String[] schemas = CassiopaeDumpUtil.getApplicationValues(CassiopaeDumpConstant.KEY_PROPERTY_SCHEMAS);
			request.getSession().setAttribute("LISTSCHEMA", schemas);
			
			getServletContext().getRequestDispatcher("/listProfile.jsp").forward(request, response);
		}else {
			getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		User userConnected = null;
		
		String login = request.getParameter("login");
		String password = request.getParameter("password");
		
		CassiopaeDumpUtil.pathToSettingFile = getServletContext().getRealPath("")+"/WEB-INF/config/";
		
		userConnected = (User) request.getSession().getAttribute("user") == null ? daoImpl.connectUser(login, password) : (User) request.getSession().getAttribute("user") ;
		
		if(userConnected!=null && userConnected.getActivateOrNot()){
			request.getSession().setAttribute("user", userConnected);
			request.getSession().setAttribute("LISTPROFIL", userConnected.getProfiles());
			
			String path = getServletContext().getRealPath("")+"/WEB-INF/config/";
			CassiopaeDumpUtil.pathToConfigFile = path;
			String[] schemas = CassiopaeDumpUtil.getApplicationValues(CassiopaeDumpConstant.KEY_PROPERTY_SCHEMAS);
			request.getSession().setAttribute("LISTSCHEMA", schemas);
			
			getServletContext().getRequestDispatcher("/listProfile.jsp").forward(request, response);
		} else {
			getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
		}
		
		
	}

}
